package com.hbnu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.OpusauthorMapper;
import com.hbnu.system.model.entity.Opus;
import com.hbnu.system.model.entity.Opusauthor;
import com.hbnu.system.service.IOpusService;
import com.hbnu.system.service.IOpusauthorService;
import com.hbnu.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@Service
public class OpusauthorServiceImpl extends ServiceImpl<OpusauthorMapper, Opusauthor> implements IOpusauthorService {
    @Autowired
    private IOpusauthorService iOpusauthorService;

    @Autowired
    private IOpusService iOpusService;

    @Autowired private IUserService iUserService;

    @Override
    public boolean addOpusauthor(Opusauthor opusAuthor) {
        Opusauthor op1 = iOpusauthorService.getOne(Wrappers.lambdaQuery(Opusauthor.class)
                .eq(Opusauthor::getOpusID,opusAuthor.getOpusID())
                .eq(Opusauthor::getUID,opusAuthor.getUID()));
        if(Objects.nonNull(op1)){
            throw new BaseException("不可重复添加");
        }
        Opus opus = iOpusService.getOne(Wrappers.lambdaQuery(Opus.class)
                .eq(Opus::getOpusID, opusAuthor.getOpusID()));
        if(Objects.isNull(opus)){
            throw new BaseException("作品不存在!");
        }

        String allauthor = opus.getAllAuthor();
        if(opus.getAllAuthor().contains(opusAuthor.getName())){
            throw new BaseException("不可重复添加!");
        }
        if(allauthor.isEmpty()||allauthor.equals(" ")){
            opus.setAllAuthor(opusAuthor.getName());
        }else {
            opus.setAllAuthor(allauthor + "、"+opusAuthor.getName());
        }
        //把相应专利patent进行修改使用update接口 更新它的相关项目这一字段
        UpdateWrapper<Opus> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("OpusID",opus.getOpusID());
        iOpusService.update(opus,updateWrapper);
        return baseMapper.insert(opusAuthor)==1;
    }

    @Override
    public boolean deleteOpusauthor(String id) {
        Opusauthor opusauthor = super.getOne(Wrappers.lambdaQuery(Opusauthor.class)
                .eq(Opusauthor::getOpusAuthorID, id));
        if(Objects.isNull(opusauthor)){
            throw new BaseException("不存在关联作者");
        }

        Opus opus = iOpusService.getOne(Wrappers.lambdaQuery(Opus.class)
                .eq(Opus::getOpusID, opusauthor.getOpusID()));
        //相关联的patent里面的字段也要进行修改 此处先把要删除的项目名称提取出来
        String author_name = opusauthor.getName();
        //此处把patent里的相关项目的字段值提取出来
        String allAuthor = opus.getAllAuthor();
        //把项目名替换成空白即可 分三种情况 分别是顿号在前在后和不存在顿号
        String front = "、" + author_name;
        String behind = author_name + "、";
        String both = "、" + author_name + "、";
        if(allAuthor.contains(both)){
            String replace = allAuthor.replace(both, "、");
            opus.setAllAuthor(replace);
        }
        else if(allAuthor.contains(front) && !allAuthor.contains(behind)){
            String replace = allAuthor.replace(front, "");
            opus.setAllAuthor(replace);
        }
        else if (allAuthor.contains(behind) && !allAuthor.contains(front)){
            String replace = allAuthor.replace(behind, "");
            opus.setAllAuthor(replace);
        }
        else if(allAuthor.contains(author_name)){
            String replace = allAuthor.replace(author_name,"");
            opus.setAllAuthor(replace);
        }


        //对paper进行更新
        UpdateWrapper<Opus> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("OpusID",opus.getOpusID());
        iOpusService.update(opus,updateWrapper);

        return baseMapper.deleteById(opusauthor) == 1;
    }

    @Override
    public boolean updateOpusauthor(Opusauthor opusauthor) {
        return baseMapper.updateById(opusauthor)==1;
    }

    @Override
    public List<Opusauthor> listAuthor(String id) {
        List<Opusauthor> list = baseMapper.selectList(Wrappers.lambdaQuery(Opusauthor.class)
                .eq(Opusauthor::getOpusID, id));
        return  list;
    }
}
